using System;
using System.Collections.Generic;
using System.Text;
using SqlTools.DataAccessLibrary;
using System.Data;
using SqlTools.SampleDAL.Data;

namespace SqlTools.SampleDAL
{
	public partial class Page : BusinessObject, IDbObject<PagePrimaryKey>
	{
		protected PagePrimaryKey _pagePrimaryKey;
		protected string _name;
		protected string _title;
		protected int _parentPageID;

		protected Page _parentPage;
		protected DbList<Page> _childPages;

		public Page()
		{
			_pagePrimaryKey = new PagePrimaryKey();
			_name = "";
			_title = "";
			_parentPageID = 0;

			_parentPage = null;
		}

		#region IDbObject<PagePrimaryKey> Members

		public PagePrimaryKey Key
		{
			get { return _pagePrimaryKey; }
		}

		#endregion

		public string Name
		{
			get { return _name; }
			set
			{
				_name = value;
				_UpdateState();
			}
		}

		public string Title
		{
			get { return _name; }
			set
			{
				_name = value;
				_UpdateState();
			}
		}

		internal int ParentPageID
		{
			get { return _parentPageID; }
		}

		public Page ParentPage
		{
			get
			{
				if ( _parentPage == null && _parentPageID != 0 )
				{
					DbPage dbPage;
					if ( !string.IsNullOrEmpty(_ConnectionStringName) )
						dbPage = new DbPage(_ConnectionStringName);
					else
						dbPage = new DbPage();

					dbPage.SelectByPK(new PagePrimaryKey(_parentPageID));
				}

				return _parentPage;
			}
			set
			{
				if ( _parentPage != value )
				{
					_parentPage = value;

					if ( _parentPage != null )
						_parentPageID = _parentPage.Key.PageID;
					else
						_parentPageID = 0;

					_UpdateState();
				}
			}
		}

		public DbList<Page> ChildPages
		{
			get
			{
				if ( _childPages == null )
				{
					DbPage dbPage;
					if ( !string.IsNullOrEmpty(_ConnectionStringName) )
						dbPage = new DbPage(_ConnectionStringName);
					else
						dbPage = new DbPage();

					_childPages = dbPage.SelectByParentPageID(this.Key.PageID);

					_childPages.ItemAdded += new DbListItemAdded<Page>(_childPages_ItemAdded);
					_childPages.ItemChanged += new DbListItemChanged<Page>(_childPages_ItemChanged);
					_childPages.ItemRemoved += new DbListItemRemoved<Page>(_childPages_ItemRemoved);
				}

				return _childPages;
			}
		}

		#region EventHandlers
		void _childPages_ItemRemoved(object sender, DbListItemRemovedEventArgs<Page> e)
		{
			if ( e.Item != null )
			{
				e.Item.ParentPage = null;
			}
		}

		void _childPages_ItemChanged(object sender, DbListItemChangedEventArgs<Page> e)
		{
			if ( e.PreviousItem != null && e.PreviousItem.ParentPage == this )
			{
				e.PreviousItem.ParentPage = null;
			}

			if ( e.NewItem != null )
			{
				e.NewItem.ParentPage = this;
			}
		}

		void _childPages_ItemAdded(object sender, DbListItemAddedEventArgs<Page> e)
		{
			if ( e.ItemAdded != null )
			{
				e.ItemAdded.ParentPage = this;
			}
		}
		#endregion

		public override void Update()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override void Insert()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override void Delete()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		protected DbPage CreateDbPage()
		{
			if ( !string.IsNullOrEmpty(_ConnectionStringName) )
				return new DbPage(_ConnectionStringName);
			else
				return new DbPage();
		}

		public static Page CreateFromReader(IDataReader reader)
		{
			Page page = new Page();

			FillFromReader(page, reader);

			return page;
		}

		public static void FillFromReader(Page page, IDataReader reader)
		{
			page.OnLoading(page);

			page.Key.PageID = (int)reader["PageID"];
			page._name = reader["Name"].ToString();
			page._title = reader["Title"].ToString();

			if ( reader["ParentPageID"] != DBNull.Value )
				page._parentPageID = (int)reader["ParentPageID"];

			page._UpdateState(ObjectState.None);

			page.OnLoaded(page);
		}
	}
}
